package com.example.helloworld;

/**
 * ClassName: SonArrTest
 * Package: com.example.helloworld
 * Description:
 *
 * @Author guotingting
 * @Create 2025/4/21 10:36 AM
 * @Version 1.0
 */
public class SonArrTest {
    public static void main(String[] args) {
        int [] arr = {1, -2, 3, 10, -4, 7, 2, -5};
        int i = getGreatestSum(arr);
        System.out.println(i);
    }

    private static int getGreatestSum(int[] arr) {
        int greatestSum = 0;
        if (arr == null || arr.length == 0) {
            return 0;
        }
        int temp = greatestSum;
        for (int i = 0; i < arr.length; i++) {
            temp += arr[i];
            if (temp < 0) {
                temp = 0;
            }
            if (temp > greatestSum) {
                greatestSum = temp;
            }
        }
        if (greatestSum == 0) {
            greatestSum = arr[0];
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] >= greatestSum) {
                    greatestSum = arr[i];
                }
            }
        }
        return greatestSum;
    }
}
